## Understanding Public Attitudes toward Restrictive Voting Laws
## Katie Clayton  
## Step 1: Read and clean data
## Last updated: June 8, 2023


# Initial settings --------------------------------------------------------

rm(list = ls())
library(tidyverse)
library(qualtRics)
library(scales)


# Read data ---------------------------------------------------------------

df <- read_survey("data/Racial+Attitudes+and+Voting+Rights+++Intraparty+Conflict+v3_June+1,+2021_07.38.csv")


# Clean data --------------------------------------------------------------

df.cleaned <- df %>% 
  filter(consent == 1) %>% 
  filter(sentry_attn == 1) %>% 
  filter(!is.na(block_assigned)) %>% 
  filter(!is.na(condition)) %>% 
  select(-c(38:64), -c(66:69), -c(72:73), -c(83:91)) %>% ## remove variables for other study
  mutate(tied_vote = replace_na(tied_vote, 0),
         terms = replace_na(terms, 0),
         electoral = replace_na(electoral, 0),
         rights = replace_na(rights, 0),
         conservative = replace_na(conservative, 0)) %>% 
  mutate(knowledge = tied_vote + terms + electoral + rights + conservative,
         knowledge = knowledge / 5) %>% 
  mutate(b1 = ifelse(block_assigned == 1, 1, 0),
         b2 = ifelse(block_assigned == 2, 1, 0),
         b3 = ifelse(block_assigned == 3, 1, 0),
         b4 = ifelse(block_assigned == 4, 1, 0),
         b5 = ifelse(block_assigned == 5, 1, 0),
         b6 = ifelse(block_assigned == 6, 1, 0),
         b7 = ifelse(block_assigned == 7, 1, 0),
         b8 = ifelse(block_assigned == 8, 1, 0)) %>% 
  mutate(rep.dum = NA,
         rep.dum = ifelse(pid == 1 & !is.na(pid), 1, rep.dum),
         rep.dum = ifelse(pid == 2 & !is.na(pid), 0, rep.dum),
         rep.dum = ifelse(pid7_ind == 1 & !is.na(pid7_ind), 1, rep.dum),
         rep.dum = ifelse(pid7_ind == 2 & !is.na(pid7_ind), 0, rep.dum)) %>% 
  mutate(vote_choice = NA,
         vote_choice = ifelse(vote_2020 == 1 & !is.na(vote_2020), 1, vote_choice),
         vote_choice = ifelse(vote_2020 == 2 & !is.na(vote_2020), 0, vote_choice)) %>% 
  mutate(support_law = rescale(support_law, to = c(0, 1))) %>% 
  mutate(don.rights = lottery_1 - lottery_2,
         don.security = lottery_1 - lottery_3)


# Sample quality check ----------------------------------------------------

mean(df.cleaned$Q_TotalDuration)
median(df.cleaned$Q_TotalDuration)
table(df.cleaned$take_serious)
table(df.cleaned$mc_vr)
table(df.cleaned$condition)
table(df.cleaned$block_assigned)
table(df.cleaned$vote_choice, df.cleaned$rep.dum)


# Principal component analysis --------------------------------------------

election.trust <- df.cleaned %>%
  select(trust_conf, counted_fairly) %>% 
  na.omit()

election.trust.pc <- prcomp(election.trust)
summary(election.trust.pc)
ltm::cronbach.alpha(election.trust)

anger <- df.cleaned %>% 
  select(angry, outraged) %>% 
  na.omit()

anger.pc <- prcomp(anger)
summary(anger.pc)
ltm::cronbach.alpha(anger)

anxious <- df.cleaned %>% 
  select(anxious, afraid) %>% 
  na.omit()

anxious.pc <- prcomp(anxious)
summary(anxious.pc)
ltm::cronbach.alpha(anxious)

happy <- df.cleaned %>% 
  select(enthusiastic, happy) %>% 
  na.omit()

happy.pc <- prcomp(happy)
summary(happy.pc)
ltm::cronbach.alpha(happy)


# Build scales ------------------------------------------------------------

df.cleaned <- df.cleaned %>% 
  rowwise() %>% 
  mutate(election.trust = mean(c(trust_conf, counted_fairly), na.rm = TRUE),
         emo.anger = mean(c(angry, outraged), na.rm = TRUE),
         emo.anxious = mean(c(anxious, afraid), na.rm = TRUE),
         emo.happy = mean(c(happy, enthusiastic), na.rm = TRUE)) %>% 
  ungroup() %>% 
  mutate(election.trust = rescale(election.trust, to = c(0, 1)),
         emo.anger = rescale(emo.anger, to = c(0, 1)),
         emo.anxious = rescale(emo.anxious, to = c(0, 1)),
         emo.happy = rescale(emo.happy, to = c(0, 1)))


# Pooling test ------------------------------------------------------------

control <- df.cleaned %>% filter(condition == "control")
placebo <- df.cleaned %>% filter(condition == "placebo")

t.test(control$support_law, placebo$support_law)
t.test(control$election.trust, placebo$election.trust)
t.test(control$emo.anger, placebo$emo.anger)


# Combine control and placebo ---------------------------------------------

df.cleaned <- df.cleaned %>% 
  mutate(group = NA,
         group = ifelse(condition == "control" | condition == "placebo" & !is.na(condition), "control", group),
         group = ifelse(condition == "harm" & !is.na(condition), "harm", group),
         group = ifelse(condition == "boost" & !is.na(condition), "boost", group))


# Write data --------------------------------------------------------------

write.csv(df.cleaned, "output/cleaned_dataset.csv")
